import ddddocr
import jsonpath
from playwright.sync_api import Page

from lib.chaojiying import ChaoJiYingClient
from lib.setting import TEMP_PATH


class LoginPage:
    def __init__(self, page: Page):
        self.page = page
        # 使用xpath语法获取用户名文本框
        self.locator_username = page.locator("//form[@id='signupForm']/input[@class='form-control uname']")
        # 密码框
        self.locator_password = page.locator("//form[@id='signupForm']/input[@class='form-control pword']")
        # 登录按钮
        self.login_button = page.locator("//button[@id='btnSubmit']")
        self.register_a_href = page.get_by_role("link", name="立即注册")

        # 必填提示
        self.username_tips = page.locator("//label[@id='username-error']")
        self.password_tips = page.locator("//label[@id='password-error']")

        # 登陆失败，5秒后弹出框，会删除，因此考虑使用，获取文本的形式进行断言
        self.login_error_alert_tips = page.get_by_text("用户不存在/密码错误")
        self.login_error_alert_tips2 = page.locator("text=用户不存在/密码错误")
        self.login_error_alert_tips3 = page.locator("//div[@class='layui-layer-content']")

        self.image_code = page.locator("//a/img[@class='imgcode']")
        self.image_code_input = page.locator("//input[@name='validateCode']")

    def navigate(self):
        self.page.goto('/login')

    def fill_username(self, username):
        # 必须先点一下才能获取到
        self.locator_username.click()
        self.locator_username.fill(username)

    def fill_password(self, password):
        self.locator_password.click()
        self.locator_password.fill(password)

    def fill_image_code(self, code):
        # self.image_code_input.click()
        self.image_code_input.fill(code)

    def check_image_code_is_visible(self):
        # 定位验证码，并截图
        image_code = TEMP_PATH.joinpath("image_code.png")
        self.image_code.screenshot(path=image_code)
        # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要
        img = open(image_code, 'rb').read()
        # 用户中心>>软件ID 生成一个替换 96001
        chaojiying = ChaoJiYingClient('xxxx', 'xxxx', 'xxxx')
        # 6001:表示计算题
        res = chaojiying.PostPic(img, 6001)
        code = jsonpath.jsonpath(res, '$.pic_str')[0]
        self.fill_image_code(code)

    def check_image_code_is_visible_dddd(self):
        """
        效果不好，识别误差很大
        :return:
        """
        ocr = ddddocr.DdddOcr(show_ad=False)
        with open(TEMP_PATH.joinpath("image_code.png"), 'rb') as img:
            img_bytes = img.read()
        res = ocr.classification(img_bytes, png_fix=True)
        self.fill_image_code(res)

    def click_login(self):
        self.login_button.click()

    def login_flow(self, username, password):
        # 定位用户名
        self.fill_username(username)
        # 定位密码
        self.fill_password(password)

        # 需要判断一下，是否存在验证码，存在验证码则调用超级鹰接口进行返回
        if self.image_code.is_visible():
            # self.check_image_code_is_visible()
            self.check_image_code_is_visible_dddd()

        # 点击登录按钮
        self.click_login()
        # 等待页面跳转，作为登录成功的标准，等待下个页面加载完成
        # self.page.wait_for_url(url='/index')

    def clean_username_password(self):
        self.fill_password('')
        self.fill_username('')
